from django.db import models, connection


class TUser(models.Model):
	user_id = models.AutoField(primary_key=True)
	user_name = models.CharField(max_length=100, blank=True, null=True)
	pwd = models.CharField(max_length=100, blank=True, null=True)

	@classmethod
	def login(cls, login_name, passwd):
		with connection.cursor() as cursor:
			cursor.execute('select user_id,user_name,pwd from t_user where user_name=%s and pwd=%s',
			               [login_name, passwd])
			result = cursor.fetchone()
			if result is None:
				return None
			else:
				user = TUser
				user.user_id = result[0]
				user.user_name = result[1]
				user.pwd = result[2]
				return user

	class Meta:
		managed = False
		db_table = 't_user'


class Description(models.Model):
	col_id = models.AutoField(primary_key=True)
	col = models.CharField(max_length=45, blank=True, null=True)
	col_cn = models.CharField(max_length=45, blank=True, null=True)
	col_abbr = models.CharField(max_length=45, blank=True, null=True)

	class Meta:
		managed = False
		db_table = 'description'
		ordering = ['col_id']


class PredictList(models.Model):
	predict_id = models.AutoField(primary_key=True)
	predict_name = models.CharField(max_length=45, blank=True, null=True)
	predict_summary = models.CharField(max_length=200, blank=True, null=True)
	predict_url = models.CharField(max_length=45, blank=True, null=True)

	class Meta:
		managed = False
		db_table = 'predict_list'
		ordering = ['predict_id']
